const config = {
  // Nuget文件存储路径
  nugetDir: "D:\\Nuget",
  // Nuget服务器地址
  nugetServer: "https://www.nuget.org",
  // Nuget API Key
  nugetApiKey: "oy2nbtrdqfcty3bwqhcqvchtkc6pygiepiaafq3mbrtkku",
};

const path = require("path");
const fs = require("fs");
const exec = require("child_process").exec;
// 输入的版本号
const version = process.argv[2];

const nugetPush = (file) => {
  const command = `dotnet nuget push ${file} -s ${config.nugetServer} ${
    config.nugetApiKey ? `-k ${config.nugetApiKey}` : ""
  } --skip-duplicate`;
  console.log(`开始推送: ${file}`);
  exec(command, (error, stdout, stderr) => {
    if (error) {
      console.error(`推送失败: ${error}`);
      return;
    }
    console.log(`推送成功: ${file}`);
  });
};

const push = () => {
  const nugetDir = config.nugetDir;
  const files = fs
    .readdirSync(nugetDir, {
      recursive: true,
    })
    .filter((file) => file.endsWith(`.${version}.nupkg`))
    .map((file) => path.join(nugetDir, file));
  for (var i in files) {
    var file = files[i];
    nugetPush(file);
  }
};

// push();

if (!version) {
  console.error("请输入要推送的版本号");
  return;
}

push();
